package com.parktech.sun.web.controller.management.template;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.sms.SmsTemplate;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.sunhz.SmsTemplateService;
import com.parktech.sun.web.controller.management.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: guo
 * Date: 11-1-10
 * Time: 下午9:58
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/management/template")
public class SmsTemplateController extends BaseController{
    @Autowired
       private SmsTemplateService smsTemplateService;

       private final String redirectListURL = "redirect:listSmsTemplates";

       @RequestMapping(value = "/listSmsTemplates", method = {RequestMethod.GET, RequestMethod.POST})
       public ModelAndView listSmsTemplates(HttpServletRequest request) {
           Integer currentPageNo = request.getParameter("pageNo") != null ? Integer.parseInt(request.getParameter("pageNo").trim()) : 1;
           ModelAndView modelAndView = new ModelAndView("management/template/smsTemplateList");

           List fieldColumns = new ArrayList();
           List searchValues = new ArrayList();
           if (request.getParameter("searchKeyName") != null) {
               fieldColumns.add("name");
               searchValues.add(request.getParameter("searchKeyName"));
           }
           Page<SmsTemplate> returnPage = new Page<SmsTemplate>(Constants.PAGINATION_PAGE_SIZE, currentPageNo);
           returnPage.setTotalCount(smsTemplateService.getTotalSizeWithSimilarCondition(SmsTemplate.class, fieldColumns, searchValues));
           returnPage.setResult(smsTemplateService.getPaginationObjectsWithSimilarCondition(SmsTemplate.class, currentPageNo, Constants.PAGINATION_PAGE_SIZE, fieldColumns, searchValues, null, null));

           return modelAndView.addObject(returnPage);
       }


       //    @ModelAttribute      @RequestParam
       @RequestMapping(value = "/manageSmsTemplate", method = RequestMethod.GET)
       public String manageSmsTemplate(Model model, HttpServletRequest request) {
           Long smsTemplateID = request.getParameter("smsTemplateID") != null ? Long.parseLong(request.getParameter("smsTemplateID").trim()) : null;
           if (smsTemplateID == null)     // request to add the new smsTemplate object
           {
               model.addAttribute(new SmsTemplate());

           } else       // request to update the certain smsTemplate object
           {
               SmsTemplate smsTemplate = smsTemplateService.getBeanByID(smsTemplateID);
               model.addAttribute(smsTemplate);

           }
           return "management/template/smsTemplateManage";
       }

       @RequestMapping(value = "/saveManageSmsTemplate", method = RequestMethod.POST)
       public String saveManageSmsTemplate(@ModelAttribute SmsTemplate smsTemplate,HttpServletRequest request) {
           Long smsTemplateID = smsTemplate.getId();
           smsTemplateService.saveOrUpdateBean(smsTemplate);
           if (smsTemplateID == null) {
               saveSuccessMessage(request,"创建成功！");
               // add the success to create message.
           } else {
               saveSuccessMessage(request,"修改成功！");
               // add the success to edit message.
           }

           return "management/template/smsTemplateManage";
       }


       @RequestMapping(value = "/deleteSmsTemplate", method = RequestMethod.GET)
       public String deleteSmsTemplate(@RequestParam("smsTemplateID") Long smsTemplateID) {
           smsTemplateService.removeBean(smsTemplateID);
           return redirectListURL;
       }

       @RequestMapping(value = "/deleteSmsTemplates", method = RequestMethod.POST)
       public String deleteSmsTemplates(HttpServletRequest request) {
           String[] smsTemplateIDArray = request.getParameter("selectedSmsTemplatesIDArray") == null ? new String[0] : request.getParameter("selectedSmsTemplatesIDArray").trim().split(",");
           for (String smsTemplateID : smsTemplateIDArray) {
               smsTemplateService.removeBean(Long.valueOf(smsTemplateID.trim()));
           }
           return redirectListURL;
       }
   }